#include "groupmodel.hpp"
#include "db.hpp"

bool GroupModel::createGroup(Group &group)
{
    // 1. 组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "insert into allgroup (groupname, groupdesc) values ('%s', '%s')", group.getName().c_str(), group.getDesc().c_str());

    // 2. 连接数据库
    MySQL mysql;
    if (mysql.connect())
    {
        if (mysql.update(sql))
        {
            // 获取插入的用户的主键id(自增)
            group.setId(mysql_insert_id(mysql.getConnection()));
            return true;
        }
    }
    return false;
}

void GroupModel::addgroup(int userid, int groupid, const std::string &role)
{
    // 1. 组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "insert into groupuser values (%d, %d, '%s')", groupid, userid, role.c_str());

    // 2. 连接数据库
    MySQL mysql;
    if (mysql.connect())
    {
        mysql.update(sql);
    }
}

std::vector<Group> GroupModel::queryGroups(int userid)
{
    // 1. 组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "select a.id, a.groupname, a.groupdesc from allgroup a inner join \
    groupuser b on a.id = b.groupid where b.userid = %d",
            userid);

    MySQL mysql;
    std::vector<Group> groupvec;
    if (mysql.connect())
    {
        MYSQL_RES *res = mysql.query(sql);
        if (nullptr != res)
        {
            MYSQL_ROW row;
            while (nullptr != (row = mysql_fetch_row(res)))
            {
                Group group;
                group.setId(atoi(row[0]));
                group.setName(row[1]);
                group.setDesc(row[2]);
                groupvec.push_back(group);
            }
            mysql_free_result(res);
        }
    }

    // 查询组内用户信息
    for (Group &group : groupvec)
    {
        sprintf(sql, "select a.id, a.name, a.state, b.grouprole from user a \
        inner join groupuser b on a.id = b.userid where b.groupid = %d",
                group.getId());

        MYSQL_RES *res = mysql.query(sql);
        if (nullptr != res)
        {
            MYSQL_ROW row;
            while (nullptr != (row = mysql_fetch_row(res)))
            {
                GroupUser user;
                user.setId(atoi(row[0]));
                user.setName(row[1]);
                user.setState(row[2]);
                user.setRole(row[3]);
                group.getUsers().push_back(user);
            }
            mysql_free_result(res);
        }
    }
    return groupvec;
}

std::vector<int> GroupModel::queryGroupUsers(int userid, int groupid)
{
    // 1. 组装sql语句
    char sql[1024] = {0};
    sprintf(sql, "select userid from groupuser where groupid = %d and userid != %d", groupid, userid);

    std::vector<int> idVec;
    MySQL mysql;
    if(mysql.connect())
    {
        MYSQL_RES *res = mysql.query(sql);
        if(nullptr != res)
        {
            MYSQL_ROW row;
            while(nullptr != (row = mysql_fetch_row(res)))
            {
                idVec.push_back(atoi(row[0]));
            }
            mysql_free_result(res);
        }
    }
    return idVec;
}
